import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import p001 from './p001';
import format from '@dfeidao/atom-web/dt/format';
import parse from '@dfeidao/atom-web/dt/parse';
import error from '@dfeidao/atom-web/msgbox/error';
import p002 from './p002';
import p003 from './p003';
export default async function a006(fd: IFeidaoAiBrowserComponent,department_no:string,project_no:string,source_type:string,work_order_status:string,startime:string,endtime:string,content:string) {

	// todo
const stime = parse(startime);
const etime = parse(endtime);
if(stime>etime){
	error("开始之间必须小于结束时间")
	return;
}else if(!stime&&!etime){
	error("请选择时间段")
	return;
}
	 
	const res = await nodejs<{res:Array<{project_no:string,create_time:number,job_content:string,project_name:string,cust_name:string,work_bill_no:string,source_type:string,work_order_status:string}>}>("test-web/zj-000003/s004",{
		filter:{
		project_no:project_no,
		content:content,
		source_type:source_type,
		work_order_status:work_order_status,
		startime:stime,
		endtime:etime
		}
	});
	

	for (let i = 0; i < res.res.length; i++) {
		if(res.res[i].source_type=="1"){
			res.res[i].source_type="总服务台";
		}else if(res.res[i].source_type=="2"){
			res.res[i].source_type="服务台";
			
		}else if(res.res[i].source_type=="3"){
			res.res[i].source_type="客户报障";
	
		}else{
			res.res[i].source_type="内部报障";
		}
		
	}
	for (let i = 0; i < res.res.length; i++) {

	if(res.res[i].work_order_status=="1"){
		res.res[i].work_order_status="结单";
	}else if(res.res[i].work_order_status=="2"){
		res.res[i].work_order_status="完成";

	}else if(res.res[i].work_order_status=="3"){
		res.res[i].work_order_status="挂起";

	}else if(res.res[i].work_order_status=="4"){
		res.res[i].work_order_status="未派单";
	}else{
		res.res[i].work_order_status="进行中";
	}
}
const ret=Array();
for (let i = 0; i < res.res.length; i++) {
	ret[i]=res.res[i].project_no;

}
const p=res.res.map((n)=>{
	return {...n,create_time:format(n.create_time,2)}
})

const  res1=await nodejs<{d1:Array<{equip_depart_no:string,project_no:string,equip_depart_name:string}>}>("test-web/zj-000003/s002",{
filter:{ 
	project_no:ret
}
});

const arr = Array()
for (let index = 0; index < res.res.length; index++) {
for (let i = 0; i < res1.d1.length; i++) {
	// if(res.dt[index].project_no==res1.d1[i].project_no){
		arr[index] = {work_bill_no:res.res[index].work_bill_no,job_content:res.res[index].job_content,source_type:res.res[index].source_type,
			cust_name:res.res[index].cust_name,project_name:res.res[index].project_name,work_order_status:res.res[index].work_order_status,create_time:p[index].create_time,equip_depart_name:res1.d1[i].equip_depart_name}

	// }
}
}

const dtNO = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p001"]');
const dtH = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p002"]');
if (arr.length > 0) {
	render(fd, arr, p001, 'p001', 'inner');
	dtNO.style.display = 'block';
	dtH.style.display = 'none';
} else {
	render(fd, '', p002, 'p002', 'inner');
	dtNO.style.display = 'none';
	dtH.style.display = 'block';
}
if (arr.length > 20) {
	render(fd, arr.length, p003, 'p003', 'inner');
}


}
